package com.jichangxiu.common.properties;

import cn.hutool.core.util.StrUtil;
import com.jichangxiu.common.enums.SourceType;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.File;

@Data
@Component
@ConfigurationProperties(prefix = "jcx")
public class JcxProperties {

    private String projectName = "Jcx-Project";

    private FileProperties fileProperties = new FileProperties();

    private ClientProperties clientProperties = new ClientProperties();

    private ServerProperties serverProperties = new ServerProperties();

    private ObjectProperties objectProperties = new ObjectProperties();

    private MenuProperties menuProperties = new MenuProperties();

    private SecurityProperties securityProperties = new SecurityProperties();

    private SwaggerProperties swaggerProperties = new SwaggerProperties();

    private MyBatisPlusProperties myBatisPlusProperties = new MyBatisPlusProperties();

    private GeneratorProperties generatorProperties = new GeneratorProperties();

    @Data
    public static class FileProperties {

        public Boolean enableLocalFileSystem = false;

        public String rootFolder = File.separator + "C:/jcx-file";

        public String actionPrefix = "view";

        public String uploadPrefix = "upload";

        public String imageFolder = "image";

        public String videoFolder = "video";

        public String audioFolder = "audio";

        public String apkFolder = "apk";

        public String fileFolder = "file";

        public String imageSuffix = "jpg,jpeg,png,gif,ico,bmp,tiff,raw";

        public String videoSuffix = "mp4,avi,rmvb,mov,flv";

        public String audioSuffix = "mp3,aac,wav,wma,cda,flac,m4a,mid,mka,mp2,mpa,mpc,ape,ofr,ogg,ra,wv,tta,ac3,dts";

        public String apkSuffix = "apk";

        public String fileSuffix = "jpg,pdf,txt,zip";

        public Integer maxSize = 1024 * 1024 * 1024;

        public String domain = "http://127.0.0.1:8081/";

    }

    @Data
    public static class ClientProperties {

        private String authToClientKey = "Authorization";

        private String jtiToClientKey = "token";

        private String userInfoToClientKey = "userInfo";

        private String tenantInfoToClientKey = "tenantInfo";

        private String roleCodeListToClientKey = "roleCodeList";

        private String powerCodeListToClientKey = "powerCodeList";

        private String postCodeListToClientKey = "postCodeList";

        private String authCodeListToClientKey = "authCodeList";

        private String captchaKeyToClientKey = "captchaKey";

        private String captchaImageToClientKey = "captchaImage";

        private String toClientStatusKey = "status";

        private Boolean toClientStatusSuccessValue = true;

        private Boolean toClientStatusFailValue = false;

        private String toClientCodeKey = "code";

        private Integer toClientCodeSuccessValue = 0;

        private Integer toClientCodeFailValue = 1;

        private String toClientMessageKey = "message";

        private String toClientMessageSuccessValue = "操作成功";

        private String toClientMessageFailValue = "操作失败";

        private String toClientDataKey = "data";

        private String toClientPageTotalKey = "total";

        private String toClientPageDataKey = "list";

    }

    @Data
    public static class ServerProperties {

        private String captchaKeyToServerKey = "captchaKey";

        private String captchaCodeToServerKey = "captchaCode";

        private String userIdKeyToServerKey = "userId";

        private String tenantIdKeyToServerKey = "tenantId";

        private String userInfoToServerKey = "userInfo";

        private String userPassToServerKey = "password";

        private String pageNumToServerKey = "pageNum";

        private String pageSizeToServerKey = "pageSize";

        private String orderByToServerKey = "orderBy";

        private String orderTypeToServerKey = "orderType";

    }

    @Data
    public static class ObjectProperties {

        private String objectIdKey = "id";

        private String objectCodeKey = "code";

        private String objectNameKey = "name";

        private String tenantCodeKey = "code";

        private String tenantNameKey = "name";

        private String sourceCodeKey = "code";

        private String deptCodeKey = "code";

        private String deptNameKey = "name";

        private String userCodeKey = "code";

        private String userNameKey = "name";

        private String postCodeKey = "code";

        private String postNameKey = "name";

        private String powerCodeKey = "code";

        private String powerNameKey = "name";

        private String roleCodeKey = "code";

        private String roleNameKey = "name";

    }

    @Data
    public static class MenuProperties {

        private String noTenantMenuCodes = "";

        private String noDeletedMenuCodes = "";

        public String getNoTenantMenuCodes() {
            String menuCodes = "Tenant,AddTenant,DelTenant,EditTenant,InfoTenant,Menu,AddMenu,DelMenu,EditMenu,EditPower,InfoMenu,InfoPower,Source,AddSource,DelSource,EditSource,InfoSource";
            return menuCodes + (noTenantMenuCodes.startsWith(",") ? noTenantMenuCodes : "," + noTenantMenuCodes);
        }

        public String getNoDeletedMenuCodes() {
            String menuCodes = "Tenant,AddTenant,DelTenant,EditTenant,InfoTenant," +
                    "Dept,AddDept,DelDept,EditDept,InfoDept," +
                    "Post,AddPost,DelPost,EditPost,InfoPost," +
                    "Role,AddRole,DelRole,EditRole,InfoRole," +
                    "User,AddUser,DelUser,EditUser,InfoUser," +
                    "Menu,AddMenu,DelMenu,EditMenu,InfoMenu,InfoPower," +
                    "Type,AddType,DelType,EditType,InfoType,AddData,DelData,EditData,InfoData," +
                    "Config,AddConfig,DelConfig,EditConfig,InfoConfig," +
                    "Source,AddSource,DelSource,EditSource,InfoSource" +
                    "Record,Log,EditLog,InfoLog,Info,EditInfo,InfoInfo";
            return menuCodes + (noDeletedMenuCodes.startsWith(",") ? noDeletedMenuCodes : "," + noDeletedMenuCodes);
        }

    }

    @Data
    public static class SecurityProperties {

        private boolean xssEnabled = true;

        private String xssExcludeUrls = "";

        private Boolean addJtiPrefix = false;

        private Boolean writeJtiToHeader = false;

        private Long authExpire = 604800L;

        private String noAuthPath;

        public String getNoAuthPath() {
            if (StrUtil.isNotEmpty(noAuthPath)) {
                noAuthPath = noAuthPath.startsWith(",") ? noAuthPath.substring(1) : noAuthPath;
                noAuthPath = noAuthPath.endsWith(",") ? noAuthPath.substring(0, noAuthPath.length() - 1) : noAuthPath;
            }
            // 默认查询用户的方法不需要权限管理
            return "/,/index,/favicon.ico,/doc.html,/login,/logout,/druid/**,/view/**,/swagger-resources/**,/v3/**,/actuator/**" + (StrUtil.isNotEmpty(noAuthPath) ? ("," + noAuthPath) : "");
        }

        private String noAuthStaticPath;

        public String getNoAuthStaticPath() {
            String STATIC_SUFFIX = ".css,.js,.png,.jpg,.gif,.jpeg,.bmp,.ico,.swf,.psd,.htc,.htm,.html,.crx,.xpi,.exe,.ipa,.apk,.woff2,.ico,.swf,.ttf,.otf,.svg,.woff,.vue";
            if (StrUtil.isNotEmpty(noAuthStaticPath)) {
                noAuthStaticPath = noAuthStaticPath.startsWith(",") ? noAuthStaticPath.substring(1) : noAuthStaticPath;
                noAuthStaticPath = noAuthStaticPath.endsWith(",") ? noAuthStaticPath.substring(0, noAuthStaticPath.length() - 1) : noAuthStaticPath;
            }
            StringBuilder noAuthStatic = new StringBuilder();
            for (String staticFile : STATIC_SUFFIX.split(",")) {
                noAuthStatic.append("/*").append(staticFile).append(",").append("/**/*").append(staticFile).append(",");
            }
            String str = noAuthStatic.toString();
            if (StrUtil.isNotEmpty(str)) {
                str = str.startsWith(",") ? str.substring(1) : str;
                str = str.endsWith(",") ? str.substring(0, str.length() - 1) : str;
            }
            return str + (StrUtil.isNotEmpty(noAuthStaticPath) ? ("," + noAuthStaticPath) : "");
        }

        private String loginProcessingUrl = "/login";

        private String logoutProcessingUrl = "/logout";

        private Boolean loginCaptchaEnabled = false;

        private Boolean loginPasswordRsaEnabled = true;

    }

    @Data
    public static class SwaggerProperties {

        // 文档标题以及分组名称
        private String titleAndGroup = "Jcx-Project";

        // 描述
        private String description = "Jcx-Project 快速开发框架 Swagger API 文档";

        // 服务器地址
        private String termsOfServiceUrl = "https://www.jichangxiu.com";

        // 作者
        private String contactName = "姬长修";

        // 主页
        private String contactUrl = "https://www.jichangxiu.com";

        // 邮箱
        private String contactEmail = "87984498@qq.com";

        // 版本
        private String version = "0.0.0";

        // 开源协议
        private String license = "MIT";

        // 开源协议地址
        private String licenseUrl = "https://www.jichangxiu.com";

        // 受管理的包名
        private String basePackage = "com.jichangxiu";

    }

    @Data
    public static class MyBatisPlusProperties {

        private String noTenantTables = "";

        private String aliasesPackage = "com.jichangxiu";

        private String mapperLocations = "classpath*:mapper/**/*.xml";

        private SourceType sourceType = SourceType.MySQL;

        private String tenantHeaderKey = "tenantId";

        private String tenantIdColumn = "tenant_id";

        public String getNoTenantTables() {
            String tableName = "sys_tenant,sys_menu,sys_power,sys_source";
            return tableName + (noTenantTables.startsWith(",") ? noTenantTables : "," + noTenantTables);
        }

    }

    @Data
    public static class GeneratorProperties {

        /**
         * 作者
         */
        public String author = "JiChangXiu";

        /**
         * 生成包路径
         */
        public String pack = "com.jichangxiu.";

        /**
         * 自动去除表前缀，默认是 false
         */
        public Boolean autoRemovePre = false;

        /**
         * 表前缀(类名不会包含表前缀)
         */
        public String tablePrefix = "sys_";

        /**
         * 默认菜单ID
         */
        public String defaultMenuId = "";

        /**
         * 默认分类ID
         */
        public String defaultCategoryId = "";

    }

}
